<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */

use \GatewayWorker\Lib\Gateway;
use \Channel\Client;

require_once __DIR__.'/Common/RouteManager.php';
require_once __DIR__.'/Common/SessionMessage.php';
require_once __DIR__.'/Common/OfflineManager.php';
require_once __DIR__.'/Public/PublicServer.php';
require_once __DIR__.'/Channel/Client.php';
require_once __DIR__.'/ProtocolApi/MsgEvents.php';
require_once __DIR__.'/DDZ/RoomManager.php';



class Events
{
    public static $PublicServer;    //公共进程
    public static $MessageServer;   //消息转发进程
    public static $DDZServer;       //斗地主进程
    public static $OfflineManager;  //离线进程

    /**
     * @param $worker
     * @throws Exception
     */
    public static function onWorkerStart($worker)
    {
        Client::connect('127.0.0.1',2206);
        switch ($worker->id) {

            //创建公共服务期进程
            case '0':
                self::$PublicServer=new PublicServer();
                Client::on(MsgEvents::$PublicServerMsg,function ($event_data){
                    self::$PublicServer->receiveMessage($event_data);
                });

                self::$OfflineManager=new OfflineManager();
                Client::on(MsgEvents::$OfflineMsg,function ($event_data){
                    self::$OfflineManager->receiveMessage($event_data);
                });
                break;

            //创建斗地主服务器进程
            case '1':
                self::$DDZServer=new RoomManager();
                Client::on(MsgEvents::$DDZServerMsg,function ($event_data){
                    self::$DDZServer->receiveMessage($event_data);
                });
                break;

        }
          
    }

    /**
     * @param $client_id
     */
    public static function onConnect($client_id)
    {
        echo $client_id."\r\n";
        $_SESSION[$client_id]=SessionMessage::createSession($client_id);
        
    }

    /**
     * @param $client_id
     * @param $message
     */
    public static function onMessage($client_id, $message)
   {
        RouteManager::onMessage($client_id,$message);
   }

    /**
     * @param $client_id
     */
   public static function onClose($client_id)
   {

   }

}
